package com.lianxueda.drawing.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lianxueda.drawing.common.R;

import com.lianxueda.drawing.domain.dto.TeacherQueryDTO;

import com.lianxueda.drawing.domain.pojo.Teachers;

import com.lianxueda.drawing.domain.vo.PageVO;
import com.lianxueda.drawing.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/v1/teacher")
@Tag(name = "教师管理模块", description = "教师管理模块接口")
@RequiredArgsConstructor
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    // 新增教师
    @Operation(summary = "新增教师")
    @PostMapping("/add")
    public R addTeacher(@RequestBody Teachers teachers) {
        teacherService.save(teachers);
        return R.suc("添加成功");
    }

    // 删除教师
    @Operation(summary = "删除教师")
    @DeleteMapping("/delete")
    public R deleteTeacher(@RequestParam Integer id) {
        teacherService.removeById(id);
        return R.suc("删除成功");
    }

    //修改教师信息
    @Operation(summary = "修改教师信息")
    @PutMapping("/update")
    public R updateTeacher(@RequestBody Teachers teachers) {
        teacherService.updateById(teachers);
        return R.suc("修改成功");
    }

    //分页查询教师列表
    @Operation(summary="分页查询教师")
    @PostMapping("/listPage")
    public R listPage(@RequestBody TeacherQueryDTO teacherQueryDTO) {
        IPage<Teachers> page = new Page<>(teacherQueryDTO.getCurPage(), teacherQueryDTO.getPageSize());
        LambdaQueryWrapper<Teachers> qw = new LambdaQueryWrapper();
        qw.like(StringUtils.isNotBlank(teacherQueryDTO.getName()), Teachers::getName, teacherQueryDTO.getName());
        qw.like(StringUtils.isNotBlank(teacherQueryDTO.getIntroduction()), Teachers::getIntroduction, teacherQueryDTO.getIntroduction());
        qw.like(StringUtils.isNotBlank(teacherQueryDTO.getContactWay()), Teachers::getContactWay, teacherQueryDTO.getContactWay());
        qw.orderByDesc(Teachers::getId);
        qw.select(Teachers.class,info -> !info.getColumn().equals("deleted"));
        IPage<Teachers> pageResult = teacherService.page(page, qw);
        PageVO<Teachers> pageVO = new PageVO<>();
        pageVO.setCurrentPage(pageResult.getCurrent());
        pageVO.setPageSize(pageResult.getSize());
        pageVO.setTotal(pageResult.getTotal());
        pageVO.setList(pageResult.getRecords());
        return R.suc(pageVO);
    }
}
